﻿IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DirectoryEntry]') AND type in (N'U'))
DROP TABLE [dbo].[DirectoryEntry]
GO

CREATE TABLE [dbo].[DirectoryEntry](
	[Id] [bigint] IDENTITY(/*--EntryIdStart--*/,/*--EntryIdStep--*/) NOT NULL,
	[ParentId] [bigint] NULL,
	[Name] [nvarchar](256) NULL,
	[CreatedOn] [datetime] NOT NULL,
	[UpdatedOn] [datetime] NOT NULL,
	[Type] [int] NOT NULL,
	CONSTRAINT [PK_DirectoryEntry] PRIMARY KEY CLUSTERED ([Id] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [FK_DirectoryEntry] ON [dbo].[DirectoryEntry] 
(
	[ParentId] ASC
) ON [PRIMARY]
GO

CREATE UNIQUE NONCLUSTERED INDEX [FK_DirectoryEntry_Name] ON [dbo].[DirectoryEntry] 
(
	[ParentId] ASC,
	[Name] ASC
) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [FK_DirectoryEntry_UpdatedOn] ON [dbo].[DirectoryEntry] 
(
	[ParentId] ASC,
	[UpdatedOn] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [FK_DirectoryEntry_CreatedOn] ON [dbo].[DirectoryEntry] 
(
	[ParentId] ASC,
	[CreatedOn] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Assembly]') AND type in (N'U'))
DROP TABLE [dbo].[Assembly]
GO

CREATE TABLE [dbo].[Assembly](
	[DirectoryEntryId] [bigint] NOT NULL,
	[Image] [varbinary](max) NOT NULL,
	CONSTRAINT [PK_Assembly] PRIMARY KEY CLUSTERED ([DirectoryEntryId] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Link]') AND type in (N'U'))
DROP TABLE [dbo].[Link]
GO

CREATE TABLE [dbo].[Link](
	[DirectoryEntryId] [bigint] NOT NULL,
	[Target] [nvarchar](max) NOT NULL,
	CONSTRAINT [PK_Link] PRIMARY KEY CLUSTERED ([DirectoryEntryId] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Message]') AND type in (N'U'))
DROP TABLE [dbo].[Message]
GO

CREATE TABLE [dbo].[Message](
	[DirectoryEntryId] [bigint] NOT NULL,
	[Data] [varbinary](max) NULL,
	[DataFormat] [int] NULL,
	CONSTRAINT [PK_Message] PRIMARY KEY CLUSTERED ([DirectoryEntryId] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Process]') AND type in (N'U'))
DROP TABLE [dbo].[Process]
GO

CREATE TABLE [dbo].[Process](
	[DirectoryEntryId] [bigint] NOT NULL,
	[Workflow] [nvarchar](1024) NOT NULL,
	CONSTRAINT [PK_Process] PRIMARY KEY CLUSTERED ([DirectoryEntryId] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Processor]') AND type in (N'U'))
DROP TABLE [dbo].[Processor]
GO

CREATE TABLE [dbo].[Processor] (
	[DirectoryEntryId] [bigint] NOT NULL,
	[Configuration] [varbinary](max) NULL,
	[ConfigurationFormat] [int] NULL,
	CONSTRAINT [PK_Processor] PRIMARY KEY CLUSTERED ([DirectoryEntryId] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ServiceContainer]') AND type in (N'U'))
DROP TABLE [dbo].[ServiceContainer]
GO

CREATE TABLE [dbo].[ServiceContainer](
	[DirectoryEntryId] [bigint] NOT NULL,
	[Configuration] [varbinary](max) NOT NULL,
	[ConfigurationFormat] [int] NOT NULL,
	CONSTRAINT [PK_ServiceContainer] PRIMARY KEY CLUSTERED ([DirectoryEntryId] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Set]') AND type in (N'U'))
DROP TABLE [dbo].[Set]
GO

CREATE TABLE [dbo].[Set](
	[DirectoryEntryId] [bigint] NOT NULL,
	[MessageType] [nvarchar](1024) NOT NULL,
	[Capacity] [int] NOT NULL,
	CONSTRAINT [PK_Set] PRIMARY KEY CLUSTERED ([DirectoryEntryId] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[State]') AND type in (N'U'))
DROP TABLE [dbo].[State]
GO

CREATE TABLE [dbo].[State](
	[DirectoryEntryId] [bigint] NOT NULL,
	[Data] [varbinary](max) NOT NULL,
	[DataFormat] [int] NOT NULL,
	CONSTRAINT [PK_State] PRIMARY KEY CLUSTERED ([DirectoryEntryId] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Workflow]') AND type in (N'U'))
DROP TABLE [dbo].[Workflow]
GO

CREATE TABLE [dbo].[Workflow](
	[DirectoryEntryId] [bigint] NOT NULL,
	[DefinitionType] [nvarchar](1024) NOT NULL,
	CONSTRAINT [PK_Workflow] PRIMARY KEY CLUSTERED ([DirectoryEntryId] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RoleUser]') AND type in (N'U'))
DROP TABLE [dbo].[RoleUser]
GO

CREATE TABLE [dbo].[RoleUser](
	[DirectoryEntryId] [bigint] NOT NULL,
	[UserName] [nvarchar](256) NOT NULL
) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[RoleUser]') AND name = N'IX_FK_RoleUser')
DROP INDEX [IX_FK_RoleUser] ON [dbo].[RoleUser] WITH ( ONLINE = OFF )
GO

CREATE NONCLUSTERED INDEX [IX_FK_RoleUser] ON [dbo].[RoleUser] 
(
	[DirectoryEntryId] ASC
) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[WorkflowPreferredProcessor]') AND type in (N'U'))
DROP TABLE [dbo].[WorkflowPreferredProcessor]
GO

CREATE TABLE [dbo].[WorkflowPreferredProcessor](
	[DirectoryEntryId] [bigint] NOT NULL,
	[ProcessorName] [nvarchar](256) NOT NULL
) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[WorkflowPreferredProcessor]') AND name = N'IX_FK_WorkflowPreferredProcessor')
DROP INDEX [IX_FK_WorkflowPreferredProcessor] ON [dbo].[WorkflowPreferredProcessor] WITH ( ONLINE = OFF )
GO

CREATE NONCLUSTERED INDEX [IX_FK_WorkflowPreferredProcessor] ON [dbo].[WorkflowPreferredProcessor] 
(
	[DirectoryEntryId] ASC
) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AccessControlList]') AND type in (N'U'))
DROP TABLE [dbo].[AccessControlList]
GO

CREATE TABLE [dbo].[AccessControlList](
	[DirectoryEntryId] [bigint] NOT NULL,
	[Role] [nvarchar](max) NOT NULL,
	[Permission] [int] NOT NULL
) ON [PRIMARY]
GO

IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[AccessControlList]') AND name = N'IX_FK_AccessControlList')
DROP INDEX [IX_FK_AccessControlList] ON [dbo].[AccessControlList] WITH ( ONLINE = OFF )
GO

CREATE NONCLUSTERED INDEX [IX_FK_AccessControlList] ON [dbo].[AccessControlList] 
(
	[DirectoryEntryId] ASC
) ON [PRIMARY]
GO
